<html>
<head>
    <meta charset="UTF-8">
    <title>智原资源管理系统</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="/layer/layer.js"></script>


</head>
<body>
<div id="app">
    <nav class="navbar navbar-inverse" style="border-radius: 0">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                        aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">智原资源管理系统</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse">

                <ul class="nav navbar-nav">

                    <li class="active">
                        <a href="/index.html">首页</a>
                    </li>
                    <li v-for="d in userResources" v-if="d.pid==0">
                        <a v-bind:href="d.url">{{d.name}}</a>
                    </li>


                </ul>
                <ul class="nav navbar-nav navbar-right hidden-sm">
                    <li><a>{{userName}}</a></li>
                    <li><a @click="logout()" href="#">退出</a></li>
                </ul>
            </div>

        </div>
    </nav>

    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <h1>用户管理</h1>

                <a href="#" type="button" class="btn btn-success" @click="addUser()">
                    添加
                </a>
                <div class="input-group" style="float: right;width: 30%">
                    <input type="text" class="form-control" placeholder="姓名" id="search" v-model="searchName">
                    <div class="input-group-btn">
                        <button class="btn btn-default" type="button" @click="searchName_()">搜索</button>
                    </div>
                </div>


            </div>
        </div>

        <div class="row">

            <div class="col-sm-12">
                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>姓名</th>
                        <th>登录名</th>
                        <th>密码</th>
                        <th>角色</th>
                        <th>创建时间</th>
                        <th>修改时间</th>
                        <th> 编辑</th>
                    </tr>
                    </thead>
                    <tbody>

                    <tr v-for="d in pageObj.list">
                        <td>{{d.id}}</td>
                        <td>{{d.name}}</td>
                        <td>{{d.loginName}}</td>
                        <td>{{d.loginPassword}}</td>
                        <td>{{d.role !=null?d.role.name:''}}</td>
                        <td>{{d.createTime}}</td>
                        <td>{{d.updateTime}}</td>
                        <td>
                            <a href="#" type="button" class="btn btn-info btn-sm" @click="updateUser(d.id)">
                                修改
                            </a>
                            <a href="#" type="button" class="btn btn-danger btn-sm" @click="deleteId(d.id)">
                                删除
                            </a>
                        </td>
                    </tr>

                    </tbody>
                </table>
                <ul class="pagination">
                    <li><a href="#" aria-label="Previous" @click="goPage(0)">上一页</a>
                    </li>

                    <li v-for="d in pageObj.pageCount " :class="d== pageObj.page?'active':''" @click="goPage(d)"><a
                            href="#">{{d}}</a>
                    </li>

                    <li>
                        <a href="#" @click="goPage(-1)">下一页 </a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>
<script>
    new Vue({
        el: "#app",
        data: {
            userResources: [],
            userName: '',
            searchName: '',
            page: 1,
            size: 10,
            pageObj: {}

        },
        created() {
            this.getUserInfo();
            this.loadData();

        },
        methods: {
            loadData() {
                var vm = this;
                $.post("/user/list", {page: vm.page, size: vm.size}, function (response) {
                    if (response.code == 200) {
                        vm.pageObj = response.data;
                    }
                })
            },

            getUserInfo() {
                var vm = this;
                $.post("/userInfo", {}, function (response) {
                    if (response.code == 200) {
                        vm.userName = response.data.name
                        vm.userResources = response.data.role.resourceList

                    } else {
                        location.href = "login.html"
                    }
                })
            },
            logout() {
                $.post("/logout", {}, function (response) {
                    location.href = "/login.html";
                })
            },
            searchName_() {
                var vm = this;
                $.post("/user/searchName", {
                    searchName: vm.searchName,
                    page: vm.page,
                    size: vm.size
                }, function (response) {
                    if (response.code == 200) {
                        vm.pageObj = response.data;
                    }
                })
            },
            goPage(d) {
                if (d == 0) {
                    this.page <= 1 ? 1 : this.page--;
                    this.loadData();
                    return;
                }
                if (d == -1) {
                    this.page >= this.pageObj.pageCount ? this.page.pageCount : this.page++;
                    this.loadData();
                    return;
                }
                this.page = d;
                this.loadData();
            },
            addUser() {
                var vm = this;
                layer.open({
                    type: 2,
                    title: '添加用户',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['500px', '80%'],
                    content: 'add.html',
                    end: function () {
                        vm.loadData();
                    }
                });
            },
            deleteId(id) {
                var vm = this;
                var index = layer.confirm('您确定要删除吗？', {
                        btn: ['确定', '取消'] //按钮
                    },
                    function () {
                        $.post("/user/deleteId", {id: id}, function (response) {
                            if (response.code == 200) {
                                vm.loadData();
                            } else {
                                layer.msg(response.msg);
                            }
                            layer.close(index)
                        })
                    }, function () {

                    });
            },
            updateUser(id) {
                var vm = this;
                layer.open({
                    type: 2,
                    title: '用户修改',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['500px', '80%'],
                    content: 'update.html?id=' + id,
                    end: function () {
                        vm.loadData();
                    }
                });
            }
        }
    });
</script>
</body>
</html>